cmake_minimum_required(VERSION 3.10)
project(SilentMouseOperation LANGUAGES CXX)

# 设置项目选项
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")

# 强制重新构建设置
set(FORCE_REBUILD ON CACHE BOOL "Force complete rebuild")

# 显示目录信息
message(STATUS "CMAKE_BINARY_DIR: ${CMAKE_BINARY_DIR}")
message(STATUS "CMAKE_CURRENT_SOURCE_DIR: ${CMAKE_CURRENT_SOURCE_DIR}")

# 定义导出宏
add_definitions(-DSILENTMOUSEOPERATION_EXPORTS)

# 创建构建标识符（每次不同）
string(TIMESTAMP BUILD_TIMESTAMP "%Y%m%d-%H%M%S")
set(BUILD_IDENTIFIER "build_${BUILD_TIMESTAMP}")
add_definitions(-DBUILD_IDENTIFIER="${BUILD_IDENTIFIER}")

# 在构建前清理上次构建的结果
add_custom_command(
    OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/clean_build.stamp
    COMMAND ${CMAKE_COMMAND} -E remove "${CMAKE_BINARY_DIR}/libSilentMouseOperation.dll"
    COMMAND ${CMAKE_COMMAND} -E remove "${CMAKE_BINARY_DIR}/libSilentMouseOperation.dll.a"
    COMMAND ${CMAKE_COMMAND} -E remove "${CMAKE_CURRENT_SOURCE_DIR}/libSilentMouseOperation.dll"
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
    COMMENT "正在清理上次构建的结果..."
    VERBATIM
)

# 创建清理目标
add_custom_target(CleanBuild
    DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/clean_build.stamp
    COMMENT "清理构建目标"
)

# 在构建前递增版本号并更新所有文件
find_package(Python3 REQUIRED)

# 创建自定义命令来递增版本号，确保在构建前执行
add_custom_command(
    OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/version_updated.stamp
    COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/increment_version.py
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
    COMMENT "正在递增版本号并更新所有相关文件..."
    VERBATIM
)
# 生成时间戳
string(TIMESTAMP COMPILE_TIME "%Y-%m-%d %H:%M:%S")
add_custom_command(
    OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/timestamp.h
    COMMAND ${CMAKE_COMMAND} -E echo "#define COMPILE_TIME \"${COMPILE_TIME}\"" > ${CMAKE_CURRENT_SOURCE_DIR}/timestamp.h
    COMMENT "Generating timestamp.h"
)
add_custom_target(timestamp DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/timestamp.h)


# 创建自定义目标来触发版本号更新
add_custom_target(IncrementVersion
    DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/version_updated.stamp
    COMMENT "版本号更新目标"
)

# 添加源文件 - 所有文件都在根目录
add_library(SilentMouseOperation SHARED
    SilentMouseOperation.cpp
    SilentMouseOperation.rc
)

# 确保清理在版本号更新之前执行，版本号更新在构建之前执行
add_dependencies(IncrementVersion CleanBuild)
add_dependencies(timestamp IncrementVersion)
add_dependencies(SilentMouseOperation timestamp)

# 强制每次构建都重新编译，避免跳过构建的问题
# 使用安全的方法：在版本号更新后强制更新源文件时间戳
add_custom_command(TARGET IncrementVersion POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_SOURCE_DIR}/SilentMouseOperation.cpp
    COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_SOURCE_DIR}/SilentMouseOperation.rc
    COMMENT "强制更新源文件时间戳以确保重新构建..."
    VERBATIM
)

# 设置目标属性确保总是构建
set_target_properties(SilentMouseOperation PROPERTIES
    EXCLUDE_FROM_ALL FALSE
)

# 为SilentMouseOperation目标添加一个总是执行的预构建命令
add_custom_command(TARGET SilentMouseOperation PRE_BUILD
    COMMAND ${CMAKE_COMMAND} -E echo "force rebuild SilentMouseOperation target..."
    COMMENT "force rebuild..."
    VERBATIM
)

# 包含当前目录（根目录）和构建目录作为头文件搜索路径
include_directories(${CMAKE_CURRENT_SOURCE_DIR})

# 设置输出目录
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)

# 链接 Windows 系统库
target_link_libraries(SilentMouseOperation PRIVATE ole32 shlwapi)

# 安装规则
install(TARGETS SilentMouseOperation
    RUNTIME DESTINATION bin
    LIBRARY DESTINATION lib
    ARCHIVE DESTINATION lib
)

install(FILES
    SilentMouseOperation_global.h
    SilentMouseOperation.h
    DESTINATION include
)

# 构建完成后自动复制所有需要的DLL文件到项目根目录
set(ALL_DLL_FILES
    "$<TARGET_FILE:SilentMouseOperation>"  # 主DLL文件
    "C:/Qt/Tools/mingw1310_64/bin/libgcc_s_seh-1.dll"
    "C:/Qt/Tools/mingw1310_64/bin/libstdc++-6.dll"
    "C:/Qt/Tools/mingw1310_64/bin/libwinpthread-1.dll"
)

# 为每个DLL文件创建单独的复制命令
foreach(DLL_FILE ${ALL_DLL_FILES})
    add_custom_command(TARGET SilentMouseOperation POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy_if_different
            "${DLL_FILE}"
            "${CMAKE_CURRENT_SOURCE_DIR}"
        COMMENT "正在复制DLL文件到项目根目录..."
        VERBATIM
    )
endforeach()
